package org.atlas.framework.helper;

import org.atlas.framework.annotation.Controller;
import org.atlas.framework.annotation.Service;
import org.atlas.framework.util.ClassUtil;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 类操作
 *
 * Created by suxinjie on 2017/3/5.
 */
public class ClassHelper {

    /**
     * 存放所有加载的类
     */
    private static final Set<Class<?>> CLASS_SET;

    static {
        CLASS_SET = ClassUtil.getClassSet(ConfigHelper.getAppBasePackage());
    }

    /**
     * 获取基础包下的所有的类
     */
    public static Set<Class<?>> getClassSet() {
        return CLASS_SET;
    }

    /**
     * 获取应用中的所有service类
     */
    public static Set<Class<?>> getServiceClassSet() {
        Set<Class<?>> classSet = new HashSet<>();
        for (Class<?> cls : CLASS_SET) {
            if (cls.isAnnotationPresent(Service.class)) {
                classSet.add(cls);
            }
        }

        return classSet;
    }

    /**
     * 获取应用中的所有controller类
     */
    public static Set<Class<?>> getControllerClassSet() {
        Set<Class<?>> classSet = new HashSet<>();
        for (Class<?> cls : CLASS_SET) {
            if (cls.isAnnotationPresent(Controller.class)) {
                classSet.add(cls);
            }
        }

        return classSet;
    }

    /**
     * 获取应用包下所有的Bean类(service,controller等)
     */
    public static Set<Class<?>> getBeanClassSet() {
        Set<Class<?>> beanClassSet = new HashSet<>();
        beanClassSet.addAll(getServiceClassSet());
        beanClassSet.addAll(getControllerClassSet());
        return beanClassSet;
    }

    /**
     * 从默认包获取实现类
     */
    public static Set<Class<?>> getClassListBySuper(Class<?> interfaceClass) {
        Set<Class<?>> implementClass = new HashSet<>();
        for (Class<?> cls : CLASS_SET) {
            if (interfaceClass.isAssignableFrom(cls) && !interfaceClass.equals(cls)) {
                implementClass.add(cls);
            }
        }

        return implementClass;
    }

}
